/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package horizon.network;

import horizon.map.Chunk;
import horizon.map.Map;
import horizon.map.Tile;
import horizon.tile.GrassTile;
import horizon.util.Vector2;

/**
 *
 * @author J_Laptop
 */
public class ChunkEncoder {

    public static NetChunk encodeChunk(Chunk c) {
        NetChunk net = new NetChunk();
        net.x = (int) c.worldPosition.getX();
        net.y = (int) c.worldPosition.getY();

        for (int i = 0; i < 100; i++) {
            Tile t = c.tiles[i];

            if (t instanceof GrassTile) {

                net.tiles[i] = 1;

            }

        }

        return net;

    }
    
    public static Chunk decodeChunk(NetChunk n){
        Vector2 worldPosition = new Vector2(n.x , n.y);
        Tile[] tiles = new Tile[100];
        
        for (int i = 0; i < 100; i++) {
           
            Tile t = new Tile("" , null);

            if (n.tiles[i] == 1) {

                t = new GrassTile("Grass" , null);

            }
            //t = new GrassTile("Grass" , null);
                
            t.setPosition(worldPosition.add(new Vector2(((int)Math.floor(i / 10)) * Map.tileSize , (i % 10) * Map.tileSize)));
            tiles[i] = t;
        }
        
        Chunk c = new Chunk(tiles , worldPosition);
        return c;
    }
}
